//@Author: wulinlin
//@Description:
//@File:  LoginController
//@Version: 1.0.0
//@Date: 2023/03/09 14:11

package na

import (
	"github.com/gin-gonic/gin"
	"pingbuqingyun_django/app/models/form"
	response2 "pingbuqingyun_django/app/models/response"
	"pingbuqingyun_django/app/services/admin"
	"pingbuqingyun_django/app/services/na"
	"pingbuqingyun_django/pkg/tools"
)

func PwdLoginHandler(ctx *gin.Context) {
	var response = response2.PwdLoginResponse{}
	pwdForm := form.PwdLoginForm{}
	err := ctx.ShouldBindJSON(&pwdForm)
	if err != nil {
		tools.HandleValidatorError(ctx, err)
		return
	}
	// 校验图片验证码

	if !na.CaptchaSer.CheckCaptcha(pwdForm.CaptchaId, pwdForm.CaptchaCode, false) {
		tools.JsonErrorStrResp(ctx, "验证码错误！")
		return
	}
	user, err := admin.UserService.LoginCheck(pwdForm.UserName, pwdForm.Password, pwdForm.LoginType)
	if err != nil {
		tools.JsonErrorResp(ctx, err)
		return
	}
	// 组装token数据
	response.Phone = user.Phone
	response.UserName = user.UserName
	response.Avatar = user.CoverImgLink
	response.IsAdmin = user.IsAdmin
	response.IsWechat = user.IsWechat
	response.Active = user.Active
	response.Token, err = admin.UserService.GetToken(user)
	if err != nil {
		tools.JsonErrorResp(ctx, err)
		return
	}
	// 组装菜单信息

	// 组装权限信息
	tools.JsonSuccessData(ctx, "登录成功！", response)
	return
}
